package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/community")
public class CommunityController extends BaseController {
    @Reference
    private CommunityService communityService;
    @Reference
    private DictService dictService;
//    分页及带条件查询
    @RequestMapping
    public String index(Map map, HttpServletRequest request){
//        获得请求参数
        Map<String, Object> filters = getFilters(request);
//        将filters放到request域中
        map.put("filters",filters);
//        调用分页方法
        PageInfo<Community> page = communityService.findPage(filters);
//        放到request中
        map.put("page",page);
//        根据编码获取北京所有的区
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        map.put("areaList",areaList);
        return "community/index";
    }
//    去添加页面
    @RequestMapping("/create")
    public String goAddPage(Map map){
        // 根据编码获取北京所有的区
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        map.put("areaList",areaList);
        return "community/create";
    }
//    添加
    @RequestMapping("/save")
    public String save(Community community){
        communityService.insert(community);
        return "common/successPage";
    }
//    去修改页面
    @RequestMapping("/edit/{id}")
    public String goEditPage(@PathVariable("id") Long id,Map map){
        List<Dict> areaList = dictService.findListByDictCode("beijing");
        map.put("areaList",areaList);
        Community community = communityService.getById(id);
        map.put("community",community);
        return "community/edit";
    }
//    修改
    @RequestMapping("/update")
    public String update(Community community){
        communityService.update(community);
        return "common/successPage";
    }
//      删除
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id){
        communityService.delete(id);
        return  "redirect:/community";
    }
}
